'use strict';
const Controller = require('egg').Controller;

/**
 *
 *
 * @class BaseController
 * @extends {Controller}
 */
class BaseController extends Controller {


  get ctx_get() {
    return this.ctx.query;
  }

  get ctx_post() {
    return this.ctx.request.body;
  }

  get user() {
    return this.ctx.session.user;
  }

  success(data) {
    this.ctx.body = {
      code: 2000,
      data,
    };
  }

  errorF(data) {
    this.ctx.body = {
      code: 404,
      data,
    };
  }

  error(data) {
    this.ctx.body = data;
  }

  notFound(msg) {
    msg = msg || 'not found';
    this.ctx.throw(404, msg);
  }

  // 检查变量
  check_param(Rule) {
    this.ctx.validate(Rule);
  }


  async add() {
    const ctx = this.ctx;
    let count = ctx.cookies.get('count');
    count = count ? Number(count) : 0;
    ctx.cookies.set('count', ++count);
    ctx.body = count;
  }

  async remove() {
    const ctx = this.ctx;
    ctx.cookies.set('count', null);
    ctx.status = 204;
  }

}
module.exports = BaseController;
